/*
    SUSAN® - Sole of Unix Save ANything

   
      Copyright (C) 2011-2016 Skyatlas Co. LTD

   

   
*/

#include "include/susan.h"
#include "lib/thread_specific_data.h"
#include "lib/thread_specific_data_key.h"
#include "include/jcr.h"

JobControlRecord* GetJcrFromThreadSpecificData()
{
  return static_cast<JobControlRecord*>(
      pthread_getspecific(ThreadSpecificDataKey::Key()));
}

void SetJcrInThreadSpecificData(JobControlRecord* jcr)
{
  int status = pthread_setspecific(ThreadSpecificDataKey::Key(), jcr);
  if (status != 0) {
    BErrNo be;
    Jmsg1(jcr, M_ABORT, 0, _("pthread_setspecific failed: ERR=%s\n"),
          be.bstrerror(status));
  }
}

void RemoveJcrFromThreadSpecificData(JobControlRecord* jcr)
{
  if (jcr == GetJcrFromThreadSpecificData()) {
    SetJcrInThreadSpecificData(nullptr);
  }
}

uint32_t GetJobIdFromThreadSpecificData()
{
  JobControlRecord* jcr = GetJcrFromThreadSpecificData();
  return (jcr ? jcr->JobId : 0);
}
